package com.example.rabbit.config;


import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * 2交换机， 3队列
 *
 * @author cool
 * @date 2022/08/09
 */
@Configurable
public class TTLQueueConfig {
    /**
     * 普通交换机名称
     */
    public static final String NORMAL_EXCHANGE_NAME = "normal_exchange";
    /**
     * 死信交换名
     */
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead_letter_exchange";
    /**
     * 正常队列a名称
     */
    public static final String NORMAL_QUEUE_A_NAME = "normal_queue_a";
    /**
     * 正常队列b名称
     */
    public static final String NORMAL_QUEUE_B_NAME = "normal_queue_b";
    /**
     * 死信队列名称
     */
    public static final String DEAD_QUEUE_NAME = "dead_letter_queue";

    public static final String ROUTING_KEY_A = "routingKeyA";
    public static final String ROUTING_KEY_B = "routingKeyB";
    public static final String ROUTING_KEY_DEAD_LETTER = "routingKeyDead";

    /**
     * =========================================== 动态 start
     * 自定义队列名称
     */
    public static final String CUSTOM_QUEUE_NAME = "custom_queue";
    public static final String CUSTOM_EXCHANGE_NAME = "custom_exchange";
    public static final String CUSTOM_ROUTING_KEY = "custom_routingKey";


    //就是靠他自定创建队列
//    @Autowired
//    RabbitAdmin rabbitAdmin;


    /**
     * 创建初始化 RabbitAdmin 对象
     *
     * @param connectionFactory 连接工厂
     * @return {@link RabbitAdmin}
     */
//    @Bean
//    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
//        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
//        rabbitAdmin.setAutoStartup(true);
//        return rabbitAdmin;
//    }

    /**
     * 创建交换机和对列
     */
//    @Bean
//    public void createExchangeQueue(DirectExchange normalExChange, DirectExchange deadLetterExChange) {
//
//        System.out.println("看看注入的对不对 normalExChange" + normalExChange.getName());
//        System.out.println("看看注入的对不对 deadLetterExChange" + deadLetterExChange.getName());
//        //死信
//        rabbitAdmin.declareExchange(normalExChange);
//        rabbitAdmin.declareQueue(getDeadLetterQueue());
//        //正常
//        rabbitAdmin.declareExchange(deadLetterExChange);
//        rabbitAdmin.declareQueue(getNormalQueueA());
//        rabbitAdmin.declareQueue(getNormalQueueB());
//    }
    @Bean(NORMAL_EXCHANGE_NAME)
    public DirectExchange getNormalExchange() {
        return new DirectExchange(NORMAL_EXCHANGE_NAME);
    }

    @Bean(DEAD_LETTER_EXCHANGE_NAME)
    public DirectExchange getDeadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }

    /**
     * 得到正常queueA 10s
     *
     * @return {@link Queue}
     */
    @Bean(NORMAL_QUEUE_A_NAME)
    public Queue getNormalQueueA() {
        Map<String, Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", ROUTING_KEY_DEAD_LETTER);
        arguments.put("x-message-ttl", 10000);
        return QueueBuilder.durable(DEAD_LETTER_EXCHANGE_NAME).withArguments(arguments).build();
    }

    /**
     * 得到正常queueB  40s
     *
     * @return {@link Queue}
     */
    @Bean(NORMAL_QUEUE_B_NAME)
    public Queue getNormalQueueB() {
        Map<String, Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", ROUTING_KEY_DEAD_LETTER);
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(DEAD_LETTER_EXCHANGE_NAME).withArguments(arguments).build();
    }

    /**
     * 得到死信队列
     *
     * @return {@link Queue}
     */
    @Bean(DEAD_QUEUE_NAME)
    public Queue getDeadLetterQueue() {
        return QueueBuilder.durable(DEAD_QUEUE_NAME).build();
    }

    @Bean
    public Binding bindingQueueA(@Qualifier(NORMAL_QUEUE_A_NAME) Queue queueA, @Qualifier(NORMAL_EXCHANGE_NAME) DirectExchange directExchange) {
        System.out.println("normalExchange: " + directExchange.toString());
        return BindingBuilder.bind(queueA).to(directExchange).with(ROUTING_KEY_A);
    }

    @Bean
    public Binding bindingQueueB(@Qualifier(NORMAL_QUEUE_B_NAME) Queue queueB, @Qualifier(NORMAL_EXCHANGE_NAME) DirectExchange directExchange) {
        return BindingBuilder.bind(queueB).to(directExchange).with(ROUTING_KEY_B);
    }

    @Bean
    public Binding bindingQueueDeadLetter(@Qualifier(DEAD_QUEUE_NAME) Queue queueDeadLetter, @Qualifier(DEAD_LETTER_EXCHANGE_NAME) DirectExchange directExchange) {
        return BindingBuilder.bind(queueDeadLetter).to(directExchange).with(ROUTING_KEY_DEAD_LETTER);
    }


    /**
     * =========================================== 自定义队列名称 动态 start
     * 过期时间由生产者发送
     */

    @Bean(CUSTOM_QUEUE_NAME)
    public Queue getCustomQueue() {
        Map<String, Object> arguments = new HashMap<>(2);
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", ROUTING_KEY_DEAD_LETTER);
        return QueueBuilder.durable(CUSTOM_QUEUE_NAME).withArguments(arguments).build();
    }
    @Bean(CUSTOM_EXCHANGE_NAME)
    public DirectExchange getCustomExchange() {
        return new DirectExchange(CUSTOM_EXCHANGE_NAME);
    }

    @Bean
    public Binding buildQueueCustom(@Qualifier(CUSTOM_QUEUE_NAME) Queue customQueue, @Qualifier(CUSTOM_EXCHANGE_NAME) DirectExchange customExchange) {
        return BindingBuilder.bind(customQueue).to(customExchange).with(CUSTOM_ROUTING_KEY);
    }
    /*
     * =========================================== 自定义队列名称 动态 end
     */

}
